<?php

namespace App\Http\Controllers\Index;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Cache;
class BaseController extends Controller
{
    //成功
    public function success($msg,$data='')
    {
        return response([
            'status'=>0,
            'msg'=>$msg,
            'data'=>$data
        ]);
    }
    //失败
    public function error($msg)
    {
        return response([
            'status'=>1,
            'msg'=>$msg
        ]);
    }
    //发送手机验证码
    public function my_send(Request $request) {//添加短信
        $phone=$request->phone;
        if(empty($phone)){
            return $this->error('手机号不能为空');
        }
        session(['phone'=>$phone]);
        $smsnum=rand(100000,999999);
        Cache::put('code'.$phone,$smsnum,5);
        $mobiles=trim($phone);
        $content=urlencode(iconv( "UTF-8", "gb2312//IGNORE" , trim("您本次注册的验证码为".$smsnum.",请尽快完成注册!【国金科技】")));
        $re = $this->SendSms($mobiles, $content);

        return  $re;
        //exit(json_encode(array("statusCode"=>200,"message"=>"发送成功！",)));

    }
    public function SendSms($mobiles, $content)
    {
        $username=urlencode(trim('guojin'));
        $password=urlencode(trim('guojin123'));
        $fp=Fopen("http://120.25.66.83:9877/api/send.aspx?username=$username&password=$password&mobiles=$mobiles&content=$content","r");
        $ret = fgetss($fp,255);
        $str = urldecode($ret);
        Fclose($fp);
        return $str;
    }
    //验证手机短信验证码
    public function verify_phone_code($phone,$code)
    {
        if(!Cache::has('code'.$phone)){
            return $this->error('验证码已过期');
        }
        $phone_code=Cache::pull('code'.$phone);
        if($phone_code!=$code){
            return $this->error('验证码不正确');
        }
        return true;
    }
}
